package com.qbccn.onlinetest.service.impl;

import com.qbccn.onlinetest.domain.MeDataPermission;
import com.qbccn.onlinetest.mapper.MeDataPermissionMapper;
import com.qbccn.onlinetest.mapper.OtCompanyMapper;
import com.qbccn.onlinetest.service.CompanyService;
import com.qbccn.onlinetest.vo.TreeCompanyVo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author linke
 * @date 2020/7/12 16:56
 * @description
 */
@Service
public class CompanyServiceImpl implements CompanyService {

  @Autowired
  private OtCompanyMapper otCompanyMapper;
  @Autowired
  private MeDataPermissionMapper meDataPermissionMapper;

  @Override
  public List<TreeCompanyVo> getCompanyAll(String username) {
    return otCompanyMapper.getCompanyAll(username);
  }

  @Override
  public List<MeDataPermission> getMyDataPermission(String username) {
    return meDataPermissionMapper.getMyDataPermission(username);
  }

  @Override
  @Transactional
  public void updateDataPermission(String username, List<String> list) {
    meDataPermissionMapper.deleteByUsername(username);
    meDataPermissionMapper.insertByUsername(username, list);
  }

  @Override
  public List<TreeCompanyVo> getCompanyData(String username, String companyCode, int level,
      Boolean findSub) {
    if (findSub) {
      ++level;
      if (meDataPermissionMapper.hasPermissionToCityLevel(username, companyCode)) { //有权限
        return meDataPermissionMapper.getLevel4Or3(companyCode, level);
      } else {
        return null;
      }
    }
    return meDataPermissionMapper.get(username, companyCode, level);
  }
}
